<template>
  <div class="password-container">
    <div class="background-image"></div>
    <el-card class="password-card">
      <div class="card-header">
        <h2>修改密码</h2>
        <p>请输入您的原密码和新密码</p>
      </div>
      <el-form :model="data.user" label-width="120px" class="password-form" :rules="rules" ref="passwordForm">
        <el-form-item label="原密码" prop="password">
          <el-input v-model="data.user.password" show-password placeholder="请输入原密码" class="form-input"/>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="data.user.newPassword" show-password placeholder="请输入新密码" class="form-input"/>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="data.user.confirmPassword" show-password placeholder="请确认新密码" class="form-input"/>
        </el-form-item>
        <div class="form-actions">
          <el-button type="primary" @click="save" class="save-button">保存修改</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue"
import request from "@/utils/request";
import {ElMessage, ElForm} from "element-plus";
import router from "@/router";

const passwordForm = ref();

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
})

const rules = {
  password: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== data.user.newPassword) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'blur' }
  ]
};

const save = async () => {
  await passwordForm.value?.validate();
  if (data.user.newPassword !== data.user.confirmPassword) {
    ElMessage.error('确认新密码错误');
    return;
  }
  request.put('/updatePassword', data.user).then(res => {
    if (res.code === '200') {
      ElMessage.success('修改密码成功');
      localStorage.removeItem('system-user');
      router.push('/login');
    } else {
      ElMessage.error(res.msg);
    }
  })
}
</script>

<style scoped>
.password-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  padding: 20px;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/imgs/修改密码背景图.png');
  background-size: cover;
  background-position: center;
  filter: blur(0);
  z-index: 1;
}

.password-card {
  width: 100%;
  max-width: 500px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  z-index: 2;
  background-color: rgba(255, 255, 255, 0.95);
}

.card-header {
  text-align: center;
  padding: 25px 0;
  border-bottom: 1px solid #f0f0f0;
}

.card-header h2 {
  margin: 0 0 8px;
  color: #333;
  font-size: 24px;
}

.card-header p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.password-form {
  padding: 30px;
}

.form-input {
  width: 100%;
  height: 42px;
  border-radius: 6px;
}

.form-actions {
  margin-top: 20px;
  text-align: center;
}

.save-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.save-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}
</style>